package controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.*;

import utils.UsersInfo;

import models.Location;
import models.PostQuestion;
import models.ReplyQuestion;
import models.SolutionAdoption;
import models.SolutionSubmission;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

@SuppressWarnings("serial")
public class ajxLocationSearchSolutionServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/xml");
		PrintWriter out = resp.getWriter();


		out.println("<TABLE><TBODY><TR><TD width=100><SPAN class='lotusBtn lotusBtnAction lotusLeft'><A href='http://opscentral.asia.ad.celestica.com/global/collaboration/SavvySol.nsf/ExportAdoptionMatrixLevel1?openagent'>Export XL</A></SPAN></TD></TR></TBODY></TABLE>");		
		// out.println("<P style='FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 14px; FONT-WEIGHT: bold' width=120px colspan='5'>Savvy Solution Adoption Matrix</P>");
		
		//tempQuery="year=" + tempval1 + "&benefit=" + tempval2 + "&location=" + tempval3 + "&customer=" + tempval4
		
		String sprocess = (String) req.getParameter("process");
		String sbenefit = (String) req.getParameter("benefits");		
		String scustomer = (String) req.getParameter("customer");
		String sregion = (String) req.getParameter("region");
		Date today = new java.util.Date();
		SimpleDateFormat formatNowYear = new SimpleDateFormat("yyyy");
		SimpleDateFormat formatNowMonth = new SimpleDateFormat("MMMMM");
		String tYear = formatNowYear.format( today ); 
		String tMonth = formatNowMonth.format(today );
		
		
		if( sprocess!= null && ( sprocess.equals("0") || sprocess.equals("") ) ){
			sprocess = null;
		}
		if( sbenefit!=null && ( sbenefit.equals("0") || sbenefit.equals("") )){
			sbenefit = null;
		}
		if( scustomer!=null && ( scustomer.equals("0")|| scustomer.equals("") ) ){
			scustomer = null;
		}
		if( sregion!=null && ( sregion.equals("0")|| sregion.equals("") ) ){
			sregion = null;
		}

		Objectify ofy = ObjectifyService.begin();		
		Query<SolutionSubmission> q = null;
		Query<SolutionAdoption> qq = null;
		boolean bProcess, bBenefit, bCustomer, bRegion;
		bProcess = bBenefit = bCustomer = bRegion = false;
		
		if( sprocess != null ){
			bProcess = true;
		}
		if( sbenefit != null ){
			bBenefit = true;
		}
		if( scustomer != null ){
			bCustomer = true;
		}
		if( sregion != null ){
			bRegion = true;
		}
		
		out.println("<B>Savvy Solutions Adopted by " + sregion + ":</B>");
		out.println("<TABLE id=insured_list1 class=tablesorter cellPadding=3 width=550>");
		out.println("<COLGROUP>");
		out.println("<COL style='WIDTH: 76px'>");
		out.println("<COL style='WIDTH: 109px'>");
		out.println("<COL style='WIDTH: 111px'>");
		out.println("<COL style='WIDTH: 92px'>");
		out.println("<COL style='WIDTH: 112px'></COLGROUP>");
		out.println("<THEAD>");
		out.println("<TR>");
		out.println("<TH class=header width=90>Solution ID</TH>");
		out.println("<TH class=header width=150>Customer</TH>");
		out.println("<TH class=header width=150>Process</TH>");
		out.println("<TH class=header width=100>Title</TH>");
		out.println("<TH class=header width=150>Expert Recommend</TH></TR></THEAD>");
		
		qq = ofy.query(SolutionAdoption.class).filter("Location", sregion);
		if ( bProcess){ qq.filter("processArea", sprocess); }		
		if( bBenefit){  qq.filter("achievedTypeDesc", sbenefit); }
		if( bCustomer){ qq.filter("Customer", scustomer); }
		
		out.println("<TBODY>");
		long recommendCount = 0;
		String tempStr  = "";
		int cnt = 0;
		
		for ( SolutionAdoption ff : qq.fetch() ) {
			
			//SEARCH THE RECOMMEND	
			RecommendConnect REC = new RecommendConnect();
			recommendCount = REC.getCount("solID", ff.getSolID());

			tempStr = "<a href='#' rel='/modules/default/expert-info-display.jsp?sol_id=" + ff.getSolID() + "' id='sticky" + cnt++ + "' title='Experts Recommended'>";
			
			out.println("<TR class=even>");
			out.println("<TD width=90><A href='/modules/default/display-adoption.jsp?adopt_id=" + ff.getAdoptID() + "'>" + ff.getAdoptCode()  + "</A></TD>");
			out.println("<TD width=150>" + ff.getCustomer() + "</TD>");
			out.println("<TD width=150>" + ff.getProcessArea() + "</TD>");
			out.println("<TD width=100>" + ff.getSolTitle() + "</TD>");
			out.println("<TD style='TEXT-ALIGN: center' width=70>" + tempStr + recommendCount + "<img src='/img/Thumbicon.gif' /></A></TD></TR>");
			//out.println("<TD style='TEXT-ALIGN: center' width=70 sizcache='2' sizset='8' jQuery1347269498656='660'><A id=msticky0 title='Experts Recommended' href='http://opscentral.asia.ad.celestica.com/global/collaboration/SavvySol.nsf/newfrmShowExpertInfo?openform&amp;parentdocid=75C67F8ADE1F6C20482576A50009A734' rel=/global/collaboration/SavvySol.nsf/newfrmShowExpertInfo?openform&amp;parentdocid=75C67F8ADE1F6C20482576A50009A734 sizcache='2' sizset='8' >0<IMG src='/img/Thumbicon.gif'>" + recommendCount + "</A> </TD></TR>");
			
		}
		out.println("</TBODY></TABLE>");
		
		out.println("<DIV style='DISPLAY: none' id=countera>" + qq.countAll() + "</DIV><BR><BR>");
		
		out.println("<BR><B>Savvy Solutions shared by " + sregion + ":</B>");
		out.println("<TABLE id=insured_list class=tablesorter cellPadding=3 width=550>");
		out.println("<COLGROUP>");
		out.println("<COL style='WIDTH: 75px'>");
		out.println("<COL style='WIDTH: 107px'>");
		out.println("<COL style='WIDTH: 118px'>");
		out.println("<COL style='WIDTH: 92px'>");
		out.println("<COL style='WIDTH: 109px'></COLGROUP>");
		out.println("<THEAD>");
		out.println("<TR>");
		out.println("<TH class=header width=90>Solution ID</TH>");
		out.println("<TH class=header width=150>Customer</TH>");
		out.println("<TH class=header width=150>Process</TH>");
		out.println("<TH class=header width=100>Title</TH>");
		out.println("<TH class=header width=150>Expert Recommend</TH></TR></THEAD>");
		out.println("<TBODY>");

		
		q = ofy.query(SolutionSubmission.class).filter("Location", sregion);
		if ( bProcess){ q.filter("processArea", sprocess); }		
		if( bBenefit){  q.filter("achievedTypeDesc", sbenefit); }
		if( bCustomer){ q.filter("Customer", scustomer); }
		
		out.println("<TBODY>");
		recommendCount = 0;
		tempStr  = "";
		cnt = 0;
		
		for ( SolutionSubmission ff : q.fetch() ) {
			
			//SEARCH THE RECOMMEND	
			RecommendConnect REC = new RecommendConnect();
			recommendCount = REC.getCount("solID", ff.getSolID());

			tempStr = "<a href='#' rel='/modules/default/expert-info-display.jsp?sol_id=" + ff.getSolID() + "' id='msticky" + cnt++ + "' title='Experts Recommended'>";
			
			out.println("<TR class=even>");
			out.println("<TD width=90><A href='/modules/default/display-solution.jsp?sol_id=" + ff.getSolID() + "'>" + ff.getSolCode() + "</A></TD>");
			out.println("<TD width=150>" + ff.getCustomer() + "</TD>");
			out.println("<TD width=150>" + ff.getProcessArea() + "</TD>");
			out.println("<TD width=100>" + ff.getTitle() + "</TD>");
			out.println("<TD style='TEXT-ALIGN: center' width=70>" + tempStr + recommendCount + "<img src='/img/Thumbicon.gif' /></A></TD></TR>");
			//out.println("<TD style='TEXT-ALIGN: center' width=70 sizcache='2' sizset='8' jQuery1347269498656='660'><A id=msticky0 title='Experts Recommended' href='http://opscentral.asia.ad.celestica.com/global/collaboration/SavvySol.nsf/newfrmShowExpertInfo?openform&amp;parentdocid=75C67F8ADE1F6C20482576A50009A734' rel=/global/collaboration/SavvySol.nsf/newfrmShowExpertInfo?openform&amp;parentdocid=75C67F8ADE1F6C20482576A50009A734 sizcache='2' sizset='8' >0<IMG src='/img/Thumbicon.gif'>" + recommendCount + "</A> </TD></TR>");
			
		}
		out.println("</TBODY></TABLE>");
		out.println("<DIV style='DISPLAY: none' id=counterb>" + q.countAll() + "</DIV>");
	}
}
